//
//  DrawRectView.m
//  stock
//
//  Created by Yangfan on 15/3/10.
//  Copyright (c) 2015年 4gread. All rights reserved.
//

#import "DrawRectView.h"

@implementation DrawRectView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setBackgroundColor:[UIColor whiteColor]];
        scale = viewWidth / 1242;
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {
    // Drawing code
    // 上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 涨幅图
    float adding = [[_dataArr objectAtIndex:0] floatValue] + [[_dataArr objectAtIndex:1] floatValue];
    float firstPersent = [[_dataArr objectAtIndex:0] floatValue] / adding;
    float secondPersent = [[_dataArr objectAtIndex:1] floatValue] / adding;
    float firstAngle = PI / 2 - secondPersent * PI;
    float secondAngle = PI - firstAngle;

    /*写文字*/
    NSString *str1 = [NSString stringWithFormat:@"涨 %@", [_dataArr objectAtIndex:0]];
    UIFont *font = [UIFont systemFontOfSize:54 * scale];
    CGSize size = [self theWordSize:str1 andFont:font andCGSize:CGSizeMake(viewWidth, viewHeight)];
    // 计算文字的位置
    [str1 drawInRect:CGRectMake(343 * scale - size.width / 2.0, 250 * scale, size.width, size.height) withAttributes:@{NSFontAttributeName: font, NSForegroundColorAttributeName: HexRGB(0xfe797f)}];
    
    // 画上面涨扇形
    CGContextSetFillColorWithColor(context, HexRGB(0xfe797f).CGColor);//填充颜色
    CGContextMoveToPoint(context, 339 * scale, 496 * scale);
    CGContextAddArc(context, 339 * scale, 496 * scale, 173*scale, secondAngle,  2 * PI + firstAngle, 0);
    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathFill); //绘制路径

    // 画下面跌扇形
    CGContextSetFillColorWithColor(context, HexRGB(0x89c997).CGColor);//填充颜色
    CGContextMoveToPoint(context, 339 * scale, 496 * scale);
    CGContextAddArc(context, 339 * scale, 496 * scale, 173*scale, firstAngle,  secondAngle, 0);
    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathFill); //绘制路径

    /*写文字*/
    NSString *str2 = [NSString stringWithFormat:@"跌 %@", [_dataArr objectAtIndex:1]];
    size = [self theWordSize:str2 andFont:font andCGSize:CGSizeMake(viewWidth, viewHeight)];
    // 计算文字的位置
    [str2 drawInRect:CGRectMake(343 * scale - size.width / 2.0, 666 * scale, size.width, size.height) withAttributes:@{NSFontAttributeName: font, NSForegroundColorAttributeName: HexRGB(0x89c997)}];
    

    // 买卖图
    adding = [[_dataArr objectAtIndex:2] floatValue] + [[_dataArr objectAtIndex:3] floatValue];
    firstPersent = [[_dataArr objectAtIndex:2] floatValue] / adding;
    secondPersent = [[_dataArr objectAtIndex:3] floatValue] / adding;
    firstAngle = PI / 2 - secondPersent * PI;
    secondAngle = PI - firstAngle;

    /*写文字*/
    NSString *str3 = [NSString stringWithFormat:@"买 %@", [_dataArr objectAtIndex:2]];
    size = [self theWordSize:str3 andFont:font andCGSize:CGSizeMake(viewWidth, viewHeight)];
    // 计算文字的位置
    [str3 drawInRect:CGRectMake(900 * scale - size.width / 2.0, 250 * scale, size.width, size.height) withAttributes:@{NSFontAttributeName: font, NSForegroundColorAttributeName: HexRGB(0xfeb64c)}];
    
    // 画上面涨扇形
    CGContextSetFillColorWithColor(context, HexRGB(0xfeb64c).CGColor);//填充颜色
    CGContextMoveToPoint(context, 900 * scale, 496 * scale);
    CGContextAddArc(context, 900 * scale, 496 * scale, 173*scale, secondAngle,  2 * PI + firstAngle, 0);
    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathFill); //绘制路径

    // 画下面跌扇形
    CGContextSetFillColorWithColor(context, HexRGB(0x99cafd).CGColor);//填充颜色
    CGContextMoveToPoint(context, 900 * scale, 496 * scale);
    CGContextAddArc(context, 900 * scale, 496 * scale, 173*scale, firstAngle,  secondAngle, 0);
    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathFill); //绘制路径

    /*写文字*/
    NSString *str4 = [NSString stringWithFormat:@"卖 %@", [_dataArr objectAtIndex:3]];
    size = [self theWordSize:str4 andFont:font andCGSize:CGSizeMake(viewWidth, viewHeight)];
    // 计算文字的位置
    [str4 drawInRect:CGRectMake(900 * scale - size.width / 2.0, 666 * scale, size.width, size.height) withAttributes:@{NSFontAttributeName: font, NSForegroundColorAttributeName: HexRGB(0x99cafd)}];

    /*底部的说明文字*/
    NSString *intro1 = @"涨跌图";
    font = [UIFont systemFontOfSize:60 * scale];
    size = [self theWordSize:intro1 andFont:font andCGSize:CGSizeMake(viewWidth, viewHeight)];
    [intro1 drawInRect:CGRectMake(343 * scale - size.width / 2.0, self.frame.size.height - size.height, size.width, size.height) withAttributes:@{NSFontAttributeName: font, NSForegroundColorAttributeName: HexRGB(0xc0bdbd)}];

    NSString *intro2 = @"买卖图";
    size = [self theWordSize:intro2 andFont:font andCGSize:CGSizeMake(viewWidth, viewHeight)];
    [intro2 drawInRect:CGRectMake(900 * scale - size.width / 2.0, self.frame.size.height - size.height, size.width, size.height) withAttributes:@{NSFontAttributeName: font, NSForegroundColorAttributeName: HexRGB(0xc0bdbd)}];
}

@end
